package com.wangsb.controller;

import com.wangsb.entity.User;
import com.wangsb.entity.UserDto;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: shenbing.wang
 * @CreateDate: 2021/2/1 10:31
 * @Version: 1.0
 * @Description:
 */
/*
 *处理请求乱码问题
 * GET:直接设置tomcat目录下conf/servlet.xml下的 <Connector URIEncoding="UTF-8">节点下 添加 URIEncoding="UTF-8"
 * POST:在servlet的时期，在获取参数之前设置，request.setCharacterEncoding("UTF-8")
 *      但是在每个方法中都去设置一遍太麻烦
 *      所以我们会用过滤器来处理编码的问题
 *
 * 使用SpringMVC提供的编码过滤器，解决POST方式中文乱码问题，
 * 在web.xml里面配置就可以
 */


@Controller
public class HelloServlet {

    @RequestMapping("/hello")
    public String hello(@RequestParam("username") String name) {
        System.out.println("hello " + name);
        return "redirect:index.jsp";
    }

    @RequestMapping("/params")
    public String params(@RequestParam("username") String username) {
        System.out.println("hello " + username);
        return "/params.jsp";
    }

    @RequestMapping("/complexParameters")
    public String complexParameters(User user) {
        System.out.println("hello " + user);
        return "/params.jsp";
    }

    @RequestMapping("/userAndRole")
    public String complexParameters(UserDto userDto) {
        System.out.println("hello " + userDto);
        return "/params.jsp";
    }

    //@RequestHeader获取请求头的信息
    @RequestMapping("/header")
    public String header(@RequestHeader("Host") String host) {
        System.out.println("hello " + host);
        return "/params.jsp";
    }
    /*
     *功能描述 获取cookie的值
     * @author shenbing.wang@hand-china.com
     * @date 2021/2/2
     * @param
     * @return 试图对象
     */

    @RequestMapping("/cookie")
    public String cookie(@CookieValue("JSESSIONID") String jsessionid) {
        System.out.println("hello " + jsessionid);
        return "/params.jsp";
    }

    @RequestMapping("/servlet")
    public String cookie(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        request.setAttribute("name", name);
        System.out.println("name = " + name);
        return "/test.jsp";
    }
}
